﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CMS;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;

public partial class Permissions_view : System.Web.UI.Page
{
    public CMS.Functions fn = new CMS.Functions();
    public StoredProcedureValues Results = new StoredProcedureValues();

    private long? PermID = 0;
    private int Action;

    protected void Page_Init(object sender, EventArgs e)
    {
        if (!CMS.Functions.IsMasterUser()) { Response.Redirect("Page.aspx"); }
        if (!CMS.Functions.VerifyLogin.Valid) { Response.Redirect("Login.aspx"); }
        else
        {
            if (Request.QueryString["PermID"] != null) { PermID = Convert.ToInt64(Request.QueryString["PermID"].ToString()); }            
            if (Request.QueryString["CMD"] != null) { Action = Convert.ToInt16(Request.QueryString["CMD"].ToString()); }

            Session.Add("PermID", PermID);
            Session.Add("CMD", Action);
        }
    }

    void FillValues()
    {
        if (Action == CMD.Update || Action == CMD.Delete)
        {
            var Permission = CMS.Permission.GetPermission(CMS.Functions.AdministrationID, PermID, null, null, null, null,null, null).FirstOrDefault();

            if (Permission != null && PermID.HasValue)
            {
                PERM_NAME.Text = Permission.PERM_NAME;
                string Value = Permission.USR_ID.Value > 0 ? Permission.USR_ID.Value.ToString() : CMS.Functions.UserID.ToString();
                dropUsername.SelectedValue = Value;
                UPL.Checked = Permission.UPL;
                USR.Checked = Permission.USR;
                PAG.Checked = Permission.PAG;
                HRS.Checked = Permission.HRS;
            }
        }

        if (Action == CMD.Delete)
        {
            DisableControls.DisableControl(this.Form);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      
        if(Action == CMD.Update)
        {
              BtnSave.Text ="Edit";
        }
        else if(Action == CMD.Delete)
        {
              BtnSave.Text ="Delete";
        }


        DropDownUsers();
        if (!IsPostBack) { FillValues(); }
    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Permissions.aspx");
    }

    void DropDownUsers()
    {
        if (!IsPostBack)
        {
            DataTable USR = CMS.StoredProcedureValues.VW_Users(null, null, null, null, CMS.Functions.AdministrationID, true, null, false);
            dropUsername.DataSource = USR;
            dropUsername.DataTextField = "USR_NAME";
            dropUsername.DataValueField = "USR_ID";
            UsernameAv.Visible = USR.Rows.Count > 0;
            dropUsername.DataBind();
        }
    }

    void ErrorMessageHandling()
    {
        ((Content)Master).Jscript = CMS.Functions.NormalPopup("User already have permissions please select a different user !", System.Drawing.KnownColor.Black);
    }

    void Values()
    {
        StoredProcedureValues Result = new StoredProcedureValues();
        if (Action == CMD.Update)
        {
            Result = CMS.Rights.InsertPermission(Action, CMS.Functions.AdministrationID, PermID, Convert.ToInt64(dropUsername.SelectedItem.Value), UPL.Checked, USR.Checked, PAG.Checked,HRS.Checked, PERM_NAME.Text);
            if (Result.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Updated succesfully !", "Permissions.aspx", System.Drawing.KnownColor.Black);
            }
            else
            if (Result.Error == 2601)
            {
                ErrorMessageHandling();
            }

        }

        if (Action == CMD.Delete)
        {
            Result = CMS.Rights.InsertPermission(Action, CMS.Functions.AdministrationID, PermID, Convert.ToInt64(dropUsername.SelectedItem.Value), UPL.Checked, USR.Checked, PAG.Checked, HRS.Checked, PERM_NAME.Text);
            if (Result.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Updated succesfully !", "Permissions.aspx", System.Drawing.KnownColor.Black);
            }
        }

        if (Action == CMD.Insert)
        {
            Result = CMS.Rights.InsertPermission(Action, CMS.Functions.AdministrationID, null, Convert.ToInt64(dropUsername.SelectedItem.Value), UPL.Checked, USR.Checked, PAG.Checked, HRS.Checked, PERM_NAME.Text);
            if (Result.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Inserted succesfully !", "Permissions.aspx", System.Drawing.KnownColor.Black);
            }
            else
            if (Result.Error == 2601)
            {
                ErrorMessageHandling();
            }
        }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
            Values();
    }
}